﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using YT.Commons;
using YT.DB;
using YT.Modules; 

namespace YT.XH.NOSE
{
    public partial class frmNSRetrunVisitMSG : Form
    {
        private DBAccess mdbcon = new DBAccess(false, false);
        public frmNSRetrunVisitMSG()
        {
            InitializeComponent();
        }
        
        private void FillGrid()
        {
            int intAddDays = 3;  //用于提前发送的天数

            //for (int intRow = 1; intRow <= 5; intRow++)
            //{   
                DataFunctions.DelTemporyTable(mdbcon, "#tmpVisit");
//                string strSQL = @"  select  A.TreatDate,C.SickName,C.HospitalName,
//                                D.Times1,D.Times2,D.Times3,D.Times4,D.Times5,
//                                D.Case1Week,D.Case2Week,D.Case3Week,D.Case4Week,D.Case5Week,
//                                D.Case1,D.Case2,D.Case3,D.Case4,D.Case5,
//                                D.TreatCaseName1,TreatCaseName2,TreatCaseName3,TreatCaseName4,TreatCaseName5,
//                                A.TreatDate as LastTreatDate   ,C.SickRecID, C.SickName      , " +
//                                " Case D.Case"+ intRow +" when '月' then dateadd(month,Case"+ intRow +"Week,B.LastDate) " +
//                                "              when '周' then dateadd(week,Case" + intRow + "Week,B.LastDate) else '' end  as SDate " +
//                                " from NoseDrTreatHistoryDtl as A inner join ( "+
//                                "select max(treatdate) as LastDate ,SickRecID from NoseDrTreatHistoryDtl group by sickrecid "+
//                                " ) B "+
//                                " on a.sickrecid=b.sickrecid and A.TreatDate=B.LastDate "+
//                                " inner join mSickPerson C on A.SickRecID=C.SickRecID "+
//                                " inner join NoseVisitWeek D on C.SickRecID=D.SickRecID " +
//                                " And (Case D.Case"+ intRow +" when '月' then dateadd(month,Case"+ intRow +"Week,B.LastDate) " +
////                                "              when '周' then dateadd(week,Case" + intRow + "Week,B.LastDate) else '' end ) <= getdate()";
//                strSQL += " and D.CurCaseSeqNo=" + intRow + " and AlreadyVisit" + intRow + " <= Times" + intRow + " ";
                string strSQL = @" select  A.TreatDate,C.SickName,C.HospitalName,                                                
                                   A.TreatDate as LastTreatDate   ,C.SickRecID, C.SickName,A.NextTreatDate as SDate   " +                                
                                "  from NoseDrTreatHistoryDtl as A inner join ( " +
                                "  select max(treatdate) as LastDate ,SickRecID from NoseDrTreatHistoryDtl group by sickrecid " +
                                "  ) B " +
                                "  on a.sickrecid=b.sickrecid and A.TreatDate=B.LastDate " +
                                "  inner join mSickPerson C on A.SickRecID=C.SickRecID and C.NeedMsg=1" +
                                "  where NextTreatDate <= getdate()";                    
                
                DataTable dtMsg = mdbcon.GetDataTable(strSQL);
                foreach (DataRow drRow in dtMsg.Rows)
                {
                                            
                }


                foreach (DataRow drRow in dtMsg.Rows)
                {
                    this.dgvMSG.Rows.Add();
                    this.dgvMSG.Rows[this.dgvMSG.Rows.Count - 1].Cells[ColSickRecID.Index].Value  = drRow["SickRecID"];
                    this.dgvMSG.Rows[this.dgvMSG.Rows.Count - 1].Cells[ColSickName.Index].Value = drRow["SickName"];
                    this.dgvMSG.Rows[this.dgvMSG.Rows.Count - 1].Cells[ColLastVisitDate.Index].Value = BaseFunctions.FormatDate( CvtFunc.ToString ( drRow["TreatDate"]), DateFormatOption.DateFmtYMD);
                    this.dgvMSG.Rows[this.dgvMSG.Rows.Count - 1].Cells[ColHispitalName.Index].Value = drRow["HospitalName"];
                    this.dgvMSG.Rows[this.dgvMSG.Rows.Count - 1].Cells[ColNextVisit.Index].Value = BaseFunctions.FormatDate(CvtFunc.ToString ( drRow["SDate"]) , DateFormatOption.DateFmtYMD );
                }

            //}
            this.dgvMSG.EndEdit();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Opacity >= 1)
            {
                timer1.Stop();
            }
            this.Opacity += 0.04;
        }

        private void frmNSRetrunVisitMSG_Load(object sender, EventArgs e)
        {
            this.Text = "应复诊提示";
            FillGrid();
            timer1.Start();
        }

        private void btnSendMsg_Click(object sender, EventArgs e)
        {
            if (this.dgvMSG.CurrentRow == null) return;
            if (CvtFunc.ToString(this.dgvMSG.CurrentRow.Cells[ColSickRecID.Index].Value)=="") return;
            
            string strSickRecId = CvtFunc.ToString(this.dgvMSG.CurrentRow.Cells[ColSickRecID.Index].Value);
            frmNSSendMsg frmSendMsg = new frmNSSendMsg(strSickRecId);
            if (strSickRecId.TrimEnd() == "") return;
            frmSendMsg.ShowDialog();
        }


    }
}
